package com.lfs.codeassistantbackend.handler;

import com.lfs.codeassistantbackend.common.Result;
import com.lfs.codeassistantbackend.exception.BizException;
import com.lfs.codeassistantbackend.exception.ErrorCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 业务异常处理
     */
    @ExceptionHandler(BizException.class)
    public Result<?> handleBizException(BizException e) {
        log.error("业务异常: {}", e.getMessage());
        return Result.error(e.getCode(), e.getMessage());
    }


    /**
     * 请求参数校验
     * @param e 校验
     * @return 统一返回结果
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public Result<?> handleMethodArgumentNotValidException(MethodArgumentNotValidException e) {
        String message = e.getBindingResult().getFieldError().getDefaultMessage();
        log.error(message);
        return Result.error(400, message);
    }

    /**
     * Spring Security 认证异常
     */
    @ExceptionHandler(org.springframework.security.authentication.BadCredentialsException.class)
    public Result<?> handleBadCredentialsException(org.springframework.security.authentication.BadCredentialsException e) {
        log.warn("认证失败: {}", e.getMessage());
        return Result.error(ErrorCode.USER_CREDENTIALS_ERROR);
    }

    /**
     * 其他异常处理
     */
    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        log.error("系统异常: ", e);
        return Result.error(ErrorCode.SYSTEM_ERROR);
    }
}
